package com.ysstech.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.service.DicvalueService;
import com.ysstech.common.util.CollectionUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.finance.entity.SubjectAdjustAuxilary;
import com.ysstech.finance.entity.SubjectProject;
import com.ysstech.finance.enums.FinanceEnum;
import com.ysstech.finance.service.SubjectAdjustAuxilaryService;
import com.ysstech.finance.service.SubjectProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 科目核算项和辅助项对应关系表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-06-27
 */
@Controller
@Slf4j
@Api(tags = "科目核算项和辅助项对应关系管理api")
@RequestMapping("/subjectAdjustAuxilary")
public class SubjectAdjustAuxilaryController extends BaseController {
    @Autowired
    private SubjectAdjustAuxilaryService subjectAdjustAuxilaryService;
    @Autowired
    private SubjectProjectService subjectProjectService;

    @YssPermissions(value = {"finance:subject:adjustAuxilary"})
    @ApiOperation(value = "跳转科目核算项和辅助项对应关系管理页面")
    @YssLog(module = "科目核算项和辅助项对应关系管理", remark = "跳转科目核算项和辅助项对应关系管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "finance/subjectAdjustAuxilary";
    }

    /**
     * 获取科目核算方案信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:subject:adjustAuxilary"})
    @ApiOperation(value = "获取科目核算方案管信息")
    @YssLog(module = "科目核算方案管管理", remark = "获取科目核算方案管信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo() throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<SubjectProject> list = subjectProjectService.list();
        map.put("listProject", list);
        return ResultGenerator.getSuccessResult("获取科目核算方案管信息成功", map);
    }

    /**
     * 获取科目核算方案信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:subject:adjustAuxilary"})
    @ApiOperation(value = "获取核算先对应的辅助项信息")
    @YssLog(module = "科目核算项和辅助项对应关系管理", remark = "获取核算先对应的辅助项信息")
    @PostMapping(value = "/queryAdjustAuxilary")
    @ResponseBody
    public RestResult queryAdjustAuxilary() throws Exception {
        //所有核算项
        List<Dicvalue> adjustCode = dicValueService.getCacheDicValueList(FinanceEnum.ADJUST_CODE.getCode());
        return ResultGenerator.getSuccessResult("获取核算先对应的辅助项信息成功", adjustCode);
    }

    /**
     * 获取科目核算方案信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:subject:adjustAuxilary"})
    @ApiOperation(value = "获取核算先对应的辅助项信息")
    @YssLog(module = "科目核算项和辅助项对应关系管理", remark = "获取核算先对应的辅助项信息")
    @PostMapping(value = "/queryAdjustAuxilaryDetail")
    @ResponseBody
    public RestResult queryAdjustAuxilaryDetail(String adjustCode) throws Exception {
        if (StringUtils.isBlank(adjustCode)) {
            return ResultGenerator.getFailResult("获取核算先对应的辅助项信息失败，请稍后重试！");
        }
        //核算对应的辅助项
        QueryWrapper<SubjectAdjustAuxilary> wrapper = new QueryWrapper<>();
        wrapper.eq("ADJUST_CODE", adjustCode);
        List<SubjectAdjustAuxilary> list = subjectAdjustAuxilaryService.list(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (SubjectAdjustAuxilary adjustAuxilary : list) {
                List<Dicvalue> dicvalueList = dicValueService.getCacheDicValueList(adjustAuxilary.getAuxilaryCode());
                if (!CollectionUtils.isEmpty(dicvalueList) && null != dicvalueList.get(0)) {
                    adjustAuxilary.setAuxilaryName(dicvalueList.get(0).getName());
                }
            }
        }
        return ResultGenerator.getSuccessResult("获取核算先对应的辅助项信息成功", list);
    }

    /**
     * 获取一个核算项的辅助项信息
     *
     * @return
     */
    @ApiOperation(value = "获取核算先对应的辅助项信息")
    @YssLog(module = "科目核算项和辅助项对应关系管理", remark = "获取核算先对应的辅助项信息")
    @PostMapping(value = "/queryAdjustAuxilaryBySubjectId")
    @ResponseBody
    public RestResult queryAdjustAuxilaryBySubjectId(SubjectAdjustAuxilary subjectAdjustAuxilary) throws Exception {
        if (null == subjectAdjustAuxilary || StringUtils.isBlank(subjectAdjustAuxilary.getAdjustCode())) {
            return ResultGenerator.getFailResult("获取核算先对应的辅助项信息失败，请稍后重试！");
        }
        return ResultGenerator.getSuccessResult("获取核算先对应的辅助项信息成功", subjectAdjustAuxilaryService.queryAdjustAuxilaryBySubjectId(subjectAdjustAuxilary));
    }
}
